package com.example.mapp.buildSql;

import com.example.entity.authority.PartyContract;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

/**
 * Created by youkun on 2018/3/23.
 */
public class PartyContractMappSql {

    public String queryByPartyIdList(PartyContract partyContract){

        String select = "a.*,b.party_name as firstPartyName,c.party_name as secondPartyName,d.user_name as addUserName ";
        String table =" party_contract as a left join party as b on a.first_party = b.id LEFT join party as c on c.id = a.second_party "
        		+ "left join sys_user d on a.add_user_id = d.id ";
        String whereParam="";
        if(partyContract.getPartyId()!=null ){
            whereParam+=" and a.party_id = #{partyId} ";
        }
        if(StringUtils.isNotBlank(partyContract.getFirstPartyName())){
            partyContract.setFirstPartyName("%"+partyContract.getFirstPartyName()+"%");
           /* partyContract.setSecondPartyName("%"+partyContract.getFirstPartyName()+"%");*/
            whereParam+=" and ( b.party_name like #{firstPartyName})";
        }
        if(StringUtils.isNotBlank(partyContract.getSecondPartyName())){
            partyContract.setSecondPartyName("%"+partyContract.getSecondPartyName()+"%");
            whereParam+=" and ( c.party_name like #{secondPartyName})";
        }
        if(partyContract.getFirstParty() != null && !"".equals(partyContract.getFirstParty())){
            whereParam+=" and a.first_party = #{firstParty} ";
        }
        if(partyContract.getSecondParty() != null && !"".equals(partyContract.getSecondParty())){
            whereParam+=" and a.second_party = #{secondParty} ";
        }
        String where = " 1=1 "+whereParam;

        String sql =  new SQL(){{
            SELECT(select);
            FROM(table);
            WHERE(where);
        }
        }.toString();
        return sql;
    }

    /**
     * @param partyContract
     * @return
     */
    public String updateContract(PartyContract partyContract){
        StringBuilder builder = new StringBuilder("update party_contract set ");
        StringBuilder setValue = new StringBuilder();
        if(partyContract.getContractNumber() != null){
            setValue.append(" contract_number=#{contractNumber},");
        }
        if(StringUtils.isNotEmpty(partyContract.getFirstParty())){
            setValue.append(" first_party=#{firstParty},");
        }
        if(StringUtils.isNotEmpty(partyContract.getContractType())){
            setValue.append(" contract_type=#{contractType},");
        }
        setValue.append(" file_list=#{fileList},");
        /*if(StringUtils.isNotEmpty(partyContract.getFileList())){

        }*/
        builder.append(setValue.substring(0,setValue.lastIndexOf(","))).append(" where 1=1 ");
        if(partyContract.getId() != null){
            builder.append(" and id = #{id}");
        }
        return builder.toString();
    }

    /**
     * 更新合同数量
     * @param partyContract
     * @return
     */
    public String updateContractNumber(PartyContract partyContract){
        StringBuilder builder = new StringBuilder("update party_contract set ");
        StringBuilder setValue = new StringBuilder();
        if(partyContract.getContractNumber() != null){
            setValue.append(" contract_number=#{contractNumber},");
        }
        builder.append(setValue.substring(0,setValue.lastIndexOf(","))).append(" where 1=1 ");
        if(partyContract.getId() != null){
            builder.append(" and id = #{id}");
        }
        return builder.toString();
    }
}
